package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.dao.ReturnListDao;
import com.atguigu.jxc.dao.ReturnListGoodsDao;
import com.atguigu.jxc.dao.UserDao;
import com.atguigu.jxc.domain.PurchaseCountVo;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.ReturnListService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ReturnListServiceImpl implements ReturnListService {
    @Autowired
    private ReturnListDao returnListDao;
    @Autowired
    private ReturnListGoodsDao returnListGoodsDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private GoodsDao goodsDao;
    /**
     * 退货单保存
     * @param returnList
     * @param returnNumber
     * @param returnListGoodsStr
     * @return
     */
    @Override
    public ServiceVO<ReturnList> saveReturnListAndGoods(ReturnList returnList, String returnNumber, String returnListGoodsStr) {
        Gson gson = new Gson();
        List<ReturnListGoods> returnListGoodsList = gson.fromJson(returnListGoodsStr,new TypeToken<List<ReturnListGoods>>(){}.getType());
        // 设置当前操作用户
        User currentUser = userDao.findUserByName((String) SecurityUtils.getSubject().getPrincipal());
        returnList.setUserId(currentUser.getUserId());
        returnList.setReturnNumber(returnNumber);
        returnListDao.insetReturnList(returnList);
        ReturnList newrReturnList = returnListDao.selectReturnListByNumber(returnNumber);
        Integer returnListId = newrReturnList.getReturnListId();
        for (ReturnListGoods returnListGoods : returnListGoodsList) {
            returnListGoods.setReturnListId(returnListId);
            returnListGoodsDao.insertReturnListGoods(returnListGoods);
            Goods goods = goodsDao.findByGoodsId(returnListGoods.getGoodsId());
            Integer num = goods.getInventoryQuantity()-returnListGoods.getGoodsNum();
            if (num >= 0){
                goods.setInventoryQuantity(num);
                goodsDao.updateGoods(goods);
            }else {
                ServiceVO<ReturnList> serviceVO = new ServiceVO<>(100, "库存不足退货失败", newrReturnList);
                return serviceVO;
            }
        }
        ServiceVO<ReturnList> serviceVO = new ServiceVO<>(100, "请求成功", newrReturnList);
        return serviceVO;
    }
    /**
     *退货单列表展示（可条件查询：单据号模糊、供应商、是否付款和日期区间）
     * @param returnNumber
     * @param supplierId
     * @param state
     * @param sTime
     * @param eTime
     * @return
     */
    @Override
    public List<ReturnList> getReturnList(String returnNumber, Integer supplierId, Integer state, String sTime, String eTime) {
        List<ReturnList> list = returnListDao.selectReturnList(returnNumber,supplierId,state,sTime,eTime);
        return list;
    }

    //4.2、显示退货单商品信息
    @Override
    public List<ReturnListGoods> getReturnListGoodsList(Integer returnListId) {
        List<ReturnListGoods> list = returnListGoodsDao.selectReturnListGoodsList(returnListId);
        return list;
    }
    //4.3、删除退货单 并删除退货单对应商品
    @Override
    @Transactional
    public ServiceVO delectReturnListAndGoods(Integer returnListId) {
        returnListGoodsDao.removeReturnListGoods(returnListId);
        returnListDao.removeReturnList(returnListId);
        ServiceVO serviceVO = new ServiceVO(100, "删除成功", null);
        return serviceVO;
    }
    //3.2、退货统计（可根据 商品类别、商品编码或名称 条件查询）
    @Override
    public String countReturnListGoods(String sTime, String eTime, Integer goodsTypeId, String codeOrName) {
        List<PurchaseCountVo> list = returnListDao.countReturnListGoods(sTime,eTime,goodsTypeId,codeOrName);
        Gson gson = new Gson();
        String json = gson.toJson(list);
        return json;
    }
    //结算退货单
    @Override
    public ServiceVO updateStateReturnList(String returnListId) {
        returnListDao.updateStateReturnList(returnListId);

        ServiceVO serviceVO = new ServiceVO(100, "支付结算成功", null);
        return serviceVO;
    }
}
